package com.melesta.payment.inapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.util.Log;
import com.google.BillingService;
import com.google.BillingService.RequestPurchase;
import com.google.BillingService.RestoreTransactions;
import com.google.Consts.PurchaseState;
import com.google.Consts.ResponseCode;
import com.google.PurchaseObserver;
import com.google.ResponseHandler;
import com.google.Security;
import com.melesta.engine.DatabaseHelper;
import com.melesta.engine.EngineActivity;
import com.melesta.payment.PaymentManager;
import com.melesta.payment.PaymentTransaction;
import com.melesta.payment.interfaces.IListener;
import com.melesta.payment.interfaces.IRequestBuilder;
import com.melesta.payment.interfaces.ISystem;

public class InAppSystem
  implements ISystem
{
  private static final String DB_INITIALIZED = "db_initialized";
  static final String SYSTEM_NAME = "inapp";
  private static final String TAG = "payment-inapp";
  private IListener defaultListener;
  private BillingService mBillingService;
  private Context mContext;
  private InAppPurchaseObserver mDungeonsPurchaseObserver;
  private Handler mHandler;
  private final String mPayloadContents = null;

  private void restoreDatabase()
  {
    if (!((Activity)this.mContext).getPreferences(0).getBoolean("db_initialized", false))
      this.mBillingService.restoreTransactions();
  }

  public void buy(InAppRequest paramInAppRequest)
  {
    String str1 = paramInAppRequest.getName();
    String str2 = paramInAppRequest.getSku();
    int i = paramInAppRequest.getCountPerItem();
    Log.d("payment-inapp", "buying: " + str1 + " sku: " + str2);
    if (!this.mBillingService.requestPurchase(str2, this.mPayloadContents))
      PaymentManager.getInstance().postResponse(new InAppResponse().setName(str1).setSku(str2).setCountPerItem(i).setStatus(InAppResponse.Status.ERROR).setErrorCode(Consts.ResponseCode.RESULT_BILLING_UNAVAILABLE.ordinal()).setListener(this.defaultListener));
  }

  public Object get(String paramString)
  {
    return null;
  }

  public IListener getDefaultListener()
  {
    return this.defaultListener;
  }

  public String getName()
  {
    return "inapp";
  }

  public IRequestBuilder getRequestBuilder()
  {
    return new InAppRequestBuilder(this);
  }

  public boolean onActivityResult(Context paramContext, int paramInt1, int paramInt2, Intent paramIntent)
  {
    return false;
  }

  public void onCreate(Context paramContext, Handler paramHandler)
  {
    BillingService.setConfigured();
    this.mHandler = paramHandler;
    this.mContext = paramContext;
    this.mDungeonsPurchaseObserver = new InAppPurchaseObserver(this.mHandler);
    this.mBillingService = new BillingService();
    this.mBillingService.setContext(paramContext);
    ResponseHandler.register(this.mDungeonsPurchaseObserver);
    if (!this.mBillingService.checkBillingSupported());
  }

  public void onDestroy(Context paramContext, Handler paramHandler)
  {
    this.mBillingService.unbind();
  }

  public void onPause(Context paramContext, Handler paramHandler)
  {
  }

  public void onResume(Context paramContext, Handler paramHandler)
  {
  }

  public void onStart(Context paramContext, Handler paramHandler)
  {
    ResponseHandler.register(this.mDungeonsPurchaseObserver);
  }

  public void onStop(Context paramContext, Handler paramHandler)
  {
  }

  public void purchaseProcessed(PaymentTransaction paramPaymentTransaction)
  {
  }

  public InAppSystem set(String paramString, Object paramObject)
  {
    if ("openkey".equals(paramString))
      Security.setPublicKey(paramObject.toString());
    return this;
  }

  public InAppSystem setDefaultListener(IListener paramIListener)
  {
    this.defaultListener = paramIListener;
    return this;
  }

  private class InAppPurchaseObserver extends PurchaseObserver
  {
    public InAppPurchaseObserver(Handler arg2)
    {
      super(localHandler);
    }

    public void onBillingSupported(boolean paramBoolean)
    {
      Log.i("payment-inapp", "supported: " + paramBoolean);
      if (paramBoolean)
        InAppSystem.this.restoreDatabase();
    }

    public void onPurchaseStateChange(Consts.PurchaseState paramPurchaseState, String paramString1, int paramInt, long paramLong, String paramString2)
    {
      Log.i("payment-inapp", "onPurchaseStateChange() itemId: " + paramString1 + " " + paramPurchaseState);
      PaymentManager localPaymentManager = PaymentManager.getInstance();
      InAppRequest localInAppRequest = (InAppRequest)localPaymentManager.getActiveRequest(new InAppRequestFilter().setSku(paramString1));
      if (localInAppRequest == null)
        return;
      if (paramPurchaseState == Consts.PurchaseState.PURCHASED)
      {
        PaymentTransaction localPaymentTransaction = new PaymentTransaction();
        localPaymentTransaction.paymentId = localInAppRequest.getName();
        localPaymentTransaction.quantity = localInAppRequest.getCountPerItem();
        localPaymentTransaction.time = System.currentTimeMillis();
        long l = DatabaseHelper.getHelper(EngineActivity.getInstance()).saveTransaction(-1L, localPaymentTransaction);
        InAppResponse localInAppResponse = new InAppResponse().setName(localInAppRequest.getName()).setSku(localInAppRequest.getSku()).setStatus(InAppResponse.Status.PURCHASE).setCountPerItem(localInAppRequest.getCountPerItem()).setListener(InAppSystem.this.defaultListener);
        localInAppResponse.setInternalId(l);
        localPaymentManager.postResponse(localInAppResponse);
      }
      localPaymentManager.removeActiveRequest(localInAppRequest);
    }

    public void onRequestPurchaseResponse(BillingService.RequestPurchase paramRequestPurchase, Consts.ResponseCode paramResponseCode)
    {
      Log.d("payment-inapp", "onRequestPurchaseResponse() itemId: " + paramRequestPurchase.mProductId + ": " + paramResponseCode);
      InAppRequest localInAppRequest = (InAppRequest)PaymentManager.getInstance().getRequest(new InAppRequestFilter().setSku(paramRequestPurchase.mProductId));
      if (localInAppRequest == null)
        return;
      if (paramResponseCode == Consts.ResponseCode.RESULT_OK)
      {
        Log.i("payment-inapp", "purchase was successfully sent to server");
        PaymentManager.getInstance().postResponse(new InAppResponse().setName(localInAppRequest.getName()).setSku(localInAppRequest.getSku()).setStatus(InAppResponse.Status.SENT).setListener(InAppSystem.this.defaultListener));
        return;
      }
      if (paramResponseCode == Consts.ResponseCode.RESULT_USER_CANCELED)
      {
        Log.i("payment-inapp", "user canceled purchase");
        PaymentManager.getInstance().postResponse(new InAppResponse().setName(localInAppRequest.getName()).setSku(localInAppRequest.getSku()).setStatus(InAppResponse.Status.CANCEL).setListener(InAppSystem.this.defaultListener));
        return;
      }
      Log.i("payment-inapp", "purchase failed");
      PaymentManager.getInstance().postResponse(new InAppResponse().setName(localInAppRequest.getName()).setSku(localInAppRequest.getSku()).setStatus(InAppResponse.Status.ERROR).setErrorCode(paramResponseCode.ordinal()).setListener(InAppSystem.this.defaultListener));
    }

    public void onRestoreTransactionsResponse(BillingService.RestoreTransactions paramRestoreTransactions, Consts.ResponseCode paramResponseCode)
    {
      if (paramResponseCode == Consts.ResponseCode.RESULT_OK)
      {
        Log.d("payment-inapp", "completed RestoreTransactions request");
        SharedPreferences.Editor localEditor = ((Activity)InAppSystem.this.mContext).getPreferences(0).edit();
        localEditor.putBoolean("db_initialized", true);
        localEditor.commit();
        return;
      }
      Log.d("payment-inapp", "RestoreTransactions error: " + paramResponseCode);
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.melesta.payment.inapp.InAppSystem
 * JD-Core Version:    0.6.2
 */